library(stargazer)
library(effects)
library(ggplot2)
library(scales)
library("grid")
library(xtable)
library(tables)
library(reporttools:)
load("final/study1.RData")

data <- study1 
data$pid <- droplevels(data$pid)
sink("final/s1_race.tex")
latex(  tabular(  (Race=race) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

sink("final/s1_gender.tex")
latex(  tabular(  (Gender=gender) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

sink("final/s1_edu.tex")
latex(  tabular(  (Education=education) ~  (Percent("col")+ 1)    ,data=data        ))
sink()

sink("final/s1_pid.tex")
latex(  tabular(  (PartyID=pid) ~  (Percent("col")+ 1)    ,data=data        ))
sink()



### Figure 1: Support for Sharing the Article
model<-glm(as.factor(include)~affectivepolarization*same+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data)
eff<-effect(model,term="affectivepolarization*same",as.table=T,default.levels=100,typical = "median",xlevels=list(affectivepolarization=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)

eff<-effect(model,term="affectivepolarization*same",as.table=T,default.levels=100,typical = "median",xlevels=list(affectivepolarization=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)
d<-ggplot(data=dataeff, aes(x=affectivepolarization, y=fit)) + geom_line() +facet_grid(~same)+ geom_ribbon(aes(ymin=lower, ymax=upper),alpha=0.2,linetype=0)+ scale_y_continuous(limits=c(0, 1))+ scale_x_continuous(limits=c(0, 1)) + xlab("Affective Polarization of the Participant")+ylab("Predicted Probability of Recommending\n Publication of the Article") + theme_bw() + scale_colour_manual(values=c("#4d4d4d","#4d4d4d")) 
d<-d + facet_grid(. ~ same) 
d
d<-d  +theme(panel.margin = unit(1, "lines")) + theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"),axis.title.x = element_text(vjust=-0.5)) +theme(legend.title=element_blank())                       
d 

ggsave(filename="final/talk.pdf", plot=d,width=9,height=4)


#\input{tables/talk_for_appendix.tex}
glm1 <- glm(include~affectivepolarization,family=binomial(logit),data=data[data$same=="Co-Partisan Criticism",])
glm2 <- glm(include~affectivepolarization+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data=data[data$same=="Co-Partisan Criticism",])
glm3 <- glm(include~affectivepolarization,family=binomial(logit),data=data[data$same=="Opposition Criticism",])
glm4 <- glm(include~affectivepolarization+gender+race+age+income+education+married+christian+pid,family=binomial(logit),data=data[data$same=="Opposition Criticism",])
stargazer(glm1,glm2,glm3,glm4,ci=T)
stargazer(glm1,glm2,glm3,glm4,covariate.labels = c("Affective Polarization","Female","White","Age","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Not Married","Not Christian","Republican","Intercept"),out = "final//talk_for_appendix.tex",dep.var.labels.include = F,no.space = T,column.labels = c("In-Party","In-Party","Out-Party","Out-Party"),dep.var.caption = "Article Criticizes:",model.numbers = F,title = "Relationship between affective polarization and supporting an article that criticizes own party or other party",star.cutoffs = c(.05,.01,.001))

#\subsection{Model separated by Party ID of respondent}

### Broken up by party
glm1 <- glm(include~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),data=data[data$same=="Co-Partisan Criticism" & data$pid=='Republican',])
glm2 <- glm(include~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),data=data[data$same=="Opposition Criticism"  & data$pid=='Republican',])


glm3 <- glm(include~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),data=data[data$same=="Co-Partisan Criticism" & data$pid=='Democrat',])
glm4 <- glm(include~affectivepolarization+gender+race+age+income+education+married+christian,family=binomial(logit),data=data[data$same=="Opposition Criticism" & data$pid=='Democrat',])

stargazer(glm1,glm2,glm3,glm4,covariate.labels = c("Affective Polarization","Female","White","Age","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Not Married","Not Christian","Intercept"),out = "final/talk_for_appendix_byparty.tex",dep.var.labels = c("Republicans","Republicans","Democrats","Democrats"),no.space = T,column.labels = c("In-Party","Out-Party","In-Party","Out-Party"),dep.var.caption = "Article Criticizes:",model.numbers = F,title = "Relationship between affective polarization and supporting an article that criticizes own party or other party",star.cutoffs = c(.05,.01,.001))
